package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TextEditActivity;
import com.github.postsanf.yinian.bean.TextKit;
import com.github.postsanf.yinian.constants.ACTIONs;

import java.util.ArrayList;
import java.util.List;

public class TextKitListAdapter extends RecyclerView.Adapter<TextKitListAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private Context context;
    private List<TextKit> datas = new ArrayList<>();
    private View mHeaderView;

    public TextKitListAdapter(Context context, List<TextKit> datas) {
        this.context = context;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView content;
        TextView source;
        RelativeLayout textkit_item;

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            textkit_item = (RelativeLayout) itemView
                    .findViewById(R.id.textkit_item);
            content = (TextView) itemView
                    .findViewById(R.id.textedit_tab_content);
            source = (TextView) itemView
                    .findViewById(R.id.textedit_kit_source);
        }

        public void bindData(final TextKit listItems) {
            if (listItems != null) {
                content.setText(listItems.getTextContent());
                source.setText("——" + listItems.getTextSource());
                textkit_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("kitContent");
                        intent.putExtra("text", listItems.getTextContent());
                        context.sendBroadcast(intent);
                        if (!TextEditActivity.mainText) {
                            context.sendBroadcast(new Intent(ACTIONs.actionTextEditKitFinish));
                        }
                    }
                });
            }
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private TextKit getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.texteditkit_item_view, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        TextKit listItems = getItem(pos);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }


    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? datas.size() : datas.size() + 1;
    }


}
